package com.chenyun.message.sms.service.client.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import com.aliyuncs.profile.DefaultProfile;

import com.chenyun.message.sms.config.SmsConfig;
import com.chenyun.message.sms.dto.SmsResponseDTO;
import com.chenyun.message.sms.dto.SmsSendReqDTO;
import com.chenyun.message.sms.dto.SmsTemplateReqDTO;
import com.chenyun.message.sms.service.client.ISmsClient;
import lombok.SneakyThrows;

/**
 * 阿里实现类
 * @author: Lonni
 * @date: 2022/8/2 0002 11:05
 */
public class AliSmsClient  implements ISmsClient {

    private volatile static IAcsClient client=null;

    /**
     * 初始化操作
     * @param smsConfig
     */
    @Override
    public void init(SmsConfig smsConfig) {
        DefaultProfile profile = DefaultProfile.getProfile(smsConfig.getRegionId(),
                smsConfig.getAccessKey(),smsConfig.getAccessSecret());
         client = new DefaultAcsClient(profile);
    }

    /**
     * 发送短信
     * @param requestDTO
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsSendRsp> doSendSms(SmsSendReqDTO requestDTO) {
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(requestDTO.getSignName());
        request.setTemplateCode(requestDTO.getTemplateCode());
        request.setPhoneNumbers(requestDTO.getPhoneNumber());
        request.setTemplateParam(JSONUtil.toJsonStr(requestDTO.getTemplateParam()));
        if (StrUtil.isNotBlank(requestDTO.getOutId())){
            request.setOutId(requestDTO.getOutId());
        }
        SendSmsResponse response = client.getAcsResponse(request);
        SmsResponseDTO.SmsSendRsp smsSendRsp = BeanUtil.copyProperties(response, SmsResponseDTO.SmsSendRsp.class);
        return SmsResponseDTO.<SmsResponseDTO.SmsSendRsp>builder()
                .success(response.getCode().equals("OK"))
                .msg(response.getMessage())
                .data(smsSendRsp)
                .build();

    }

    /**
     * 同步短信模板
     * @param templateDTO
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsAddTemplateRsp> syncSmsTemplate(SmsTemplateReqDTO templateDTO) {
        if (StrUtil.isNotBlank(templateDTO.getTemplateCode())){
            return this.updateTemplate(templateDTO);
        }
        AddSmsTemplateRequest request=new AddSmsTemplateRequest();
        request.setTemplateType(templateDTO.getTemplateType().ordinal());
        request.setTemplateName(templateDTO.getTitle());
        request.setTemplateContent(templateDTO.getContent());
        request.setRemark(templateDTO.getRemark());
        AddSmsTemplateResponse response = client.getAcsResponse(request);
        SmsResponseDTO.SmsAddTemplateRsp rsp=BeanUtil.copyProperties(response,SmsResponseDTO.SmsAddTemplateRsp.class);
        return SmsResponseDTO.<SmsResponseDTO.SmsAddTemplateRsp>builder()
                .success(response.getCode().equals("OK"))
                .msg(response.getMessage())
                .data(rsp)
                .build();

    }

    /**
     * 修改审核未通过的
     * @param templateDTO
     * @return
     */
     @SneakyThrows
     SmsResponseDTO<SmsResponseDTO.SmsAddTemplateRsp> updateTemplate(SmsTemplateReqDTO templateDTO){
         ModifySmsTemplateRequest request=new ModifySmsTemplateRequest();
         request.setTemplateType(templateDTO.getTemplateType().ordinal());
         request.setTemplateName(templateDTO.getTitle());
         request.setTemplateContent(templateDTO.getContent());
         request.setRemark(templateDTO.getRemark());
         ModifySmsTemplateResponse response = client.getAcsResponse(request);
         SmsResponseDTO.SmsAddTemplateRsp rsp=BeanUtil.copyProperties(response,SmsResponseDTO.SmsAddTemplateRsp.class);
         return SmsResponseDTO.<SmsResponseDTO.SmsAddTemplateRsp>builder()
                 .success(response.getCode().equals("OK"))
                 .msg(response.getMessage())
                 .data(rsp)
                 .build();
     }

    /**
     * 查询短信模板的审核状态
     *
     * @param templateCode
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsTemplateStatusRsp> querySmsTemplateStatus(String templateCode) {
        Assert.notBlank(templateCode,"模板编号不能为空!");
        QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
        QuerySmsTemplateResponse response = client.getAcsResponse(request);
        SmsResponseDTO.SmsTemplateStatusRsp rsp=BeanUtil.copyProperties(response,SmsResponseDTO.SmsTemplateStatusRsp.class);
        return SmsResponseDTO.<SmsResponseDTO.SmsTemplateStatusRsp>builder()
                .success(response.getCode().equals("OK"))
                .msg(response.getMessage())
                .data(rsp)
                .build();
    }

    /**
     * 删除短信模版
     *
     * @param templateCode
     * @return
     */
    @SneakyThrows
    @Override
    public SmsResponseDTO<SmsResponseDTO.SmsAddTemplateRsp> delSmsTemplate(String templateCode) {
        Assert.notBlank(templateCode,"模板编号不能为空!");
        DeleteSmsTemplateRequest request = new DeleteSmsTemplateRequest();
        request.setTemplateCode(templateCode);
        DeleteSmsTemplateResponse response = client.getAcsResponse(request);
        SmsResponseDTO.SmsAddTemplateRsp rsp=BeanUtil.copyProperties(response,SmsResponseDTO.SmsAddTemplateRsp.class);
        return SmsResponseDTO.<SmsResponseDTO.SmsAddTemplateRsp>builder()
                .success(response.getCode().equals("OK"))
                .msg(response.getMessage())
                .data(rsp)
                .build();
    }
}
